< previous page page_204 next page >

Page 204
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
version. Because of this driver dependence, it is very important for applications to query the driver for the correct size of the DEVMODE structure before allocating a buffer for it.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
To make changes to print settings that are local to an application, an application should follow these steps:
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
1. Get the number of bytes required for the full DEVMODE structure by calling DocumentProperties and specifying zero in the fMode parameter.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
2. Allocate memory for the full DEVMODE structure.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
3. Get the current printer settings by calling DocumentProperties. Pass a pointer to the DEVMODE structure allocated in Step 2 as the pDevModeOutput parameter and specify the DM_OUT_BUFFER value.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
4. Modify the appropriate members of the returned DEVMODE structure and indicate which members were changed by setting the corresponding bits in the dmFields member of the DEVMODE.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
5. Call DocumentProperties and pass the modified DEVMODE structure back as both the pDevModeInput and pDevModeOutput parameters and specify both the DM_IN_BUFFER and DM_OUT_BUFFER values (which are combined using the OR operator).
As you can see, even though the C function declaration describes the pDevModeInput and pDevModeOutput parameters as type PDEVMODE (pointer to a DEVMODE structure), a close reading of the function description makes it clear that this is not the whole story. In fact, these parameters are pointers to memory buffers that have a DEVMODE structure at the start of the buffer, followed by additional device-specific data. From the C perspective, describing these parameters as the PDEVMODE data type is entirely correctthe parameter is, in fact, a pointer to a DEVMODE structure. It just happens to have additional data after the structure.
From the Visual Basic perspective, things are a bit more complicated. If you declare the parameter type as a DEVMODE structure, you won't be including the additional data. Some printer drivers don't use private data in the DEVMODE structure. These printers will work just fine with the code in the puzzle. But those drivers that do use the private data will attempt to access memory that is outside of the structure. In some cases, this will have no effect or will just lead to some strange printer settings. In other cases, you may get a memory exception, your

 
< previous page page_204 next page >